﻿/*

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in 
the Software without restriction, including without limitation the rights to 
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 
of the Software, and to permit persons to whom the Software is furnished to do 
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all 
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
SOFTWARE.

*/



/*

To execute, open desired FLA with missing fonts and double click this file to open in Flash.

To install as a command, copy this file into:

Operating system	Location
------------------------------
Windows 7			boot drive\Users\username\AppData\Local\Adobe\Flash CS6\language\Configuration\Commands
Windows Vista		boot drive\Users\username\Local Settings\Application Data\Adobe\Flash CS6\language\Configuration\Commands
Windows XP			boot drive\Documents and Settings\user\Local Settings\Application Data\Adobe\Flash CS6\language\Configuration\Commands
Mac OS X			Macintosh HD/Users/userName/Library/Application Support/Adobe/Flash CS6/language/Configuration/Commands

It will automatically appear in the Commands menu in Flash.

*/



/*

This JSFL command iterates through every library item in an FLA and finds 
dynamic and input TextField instances that have their font property set to a
font that isn't embedded in the FLA.

The logic flow of this Command is:

1. Scan the library and create a list of embedded fonts.
2. Iterate through all library items to find MovieClips, Graphics, and Buttons.
3. Look for dynamic and input TextField instances.
4. Verify the TextField's font is in the list of embedded fonts
5. If the font is not listed, print its info to the output buffer.

*/



// ----------------------------------------------------------------------------
// Init



function findMissingFonts()
{
	var all_items = dom.library.items;
	
	var embedded_fonts = [];
	
	var missing_font_count = 0;
	
	
	fl.outputPanel.clear();
	fl.outputPanel.trace("Scanning \"" + dom.name + "\"");
	fl.outputPanel.trace("===============================================================================");
	
	
	
	// ----------------------------------------------------------------------------
	// Find embedded fonts
	
	
	
	fl.outputPanel.trace("");
	fl.outputPanel.trace("");
	fl.outputPanel.trace("Embedded fonts");
	fl.outputPanel.trace("=======================================");
	
	
	var i = all_items.length;
	
	
	while(i--)
	{
		var library_item = all_items[i];
		
		
		if("font" != library_item.itemType)
		{
			continue;
		}
		
		
		var font_name = library_item.name;
		
		
		// Remove folder from font name if applicable.
		// 
		if(-1 != font_name.indexOf("/"))
		{
			font_name = font_name.substr(font_name.lastIndexOf("/") + 1);
		}
		
		
		embedded_fonts.push(font_name);
		
		fl.outputPanel.trace(font_name);
	}
	
	
	
	// ----------------------------------------------------------------------------
	// Find all TextField instances
	
	
	
	fl.outputPanel.trace("");
	fl.outputPanel.trace("");
	fl.outputPanel.trace("TextFields missing fonts");
	fl.outputPanel.trace("=======================================");
	
	
	var i = all_items.length;
	
	
	// Library items
	// 
	while(i--)
	{
		var library_item = all_items[i];
		
		
		// Is the current library item worth scanning?
		// 
		if("movie clip" != library_item.itemType && "graphic" != library_item.itemType && "button" != library_item.itemType)
		{
			continue;
		}
		
		
		var layers = library_item.timeline.layers;
		var j = layers.length;
		
		var instance_names = [];
		
		var buffer = library_item.symbolType + ": " + library_item.name + "\n";
		
		
		// Layers
		// 
		while(j--)
		{
			var layer = layers[j];
			var frames = layer.frames;
			var k = frames.length;
			
			
			// Frames
			// 
			while(k--)
			{
				var elements = frames[k].elements;
				var m = elements.length;
				
				var missing_font_found = false;
				
				
				// Stage elements
				// 
				elementLoop: while(m--)
				{
					var element = elements[m];
					
					
					// --------------------------------
					// Stage instance type check
					
					
					if("text" != element.elementType)
					{
						continue;
					}
					
					if("static" == element.textType)
					{
						continue;
					}
					
					
					// --------------------------------
					// Duplicate instance name check
					
					
					var instance_name = element.name ? element.name : "unnamed";
					
					
					// Has this TextField's instance name has already been mentioned?
					// 
					if("unnamed" != instance_name && -1 != instance_names.indexOf(instance_name))
					{
						continue elementLoop;
					}
					
					
					// --------------------------------
					// Missing font check
					
					
					var font_name = element.getTextAttr("face").replace("*", "");
					
					
					// Is this TextField's font embedded properly?
					// 
					if(-1 != embedded_fonts.indexOf(font_name))
					{
						continue elementLoop;
					}
					
					
					// --------------------------------
					// Missing font exists
					
					
					missing_font_found = true;
					
					instance_names.push(instance_name);
					
					
					buffer += "   ";
					buffer += fill(instance_name, 30, " ");
					buffer += fill(font, 50, " ");
					buffer += fill(layer.name, 30, " ");
					buffer += "\n";
				}
				
				
				// --------------------------------
				// Output if applicable
				
				
				if(missing_font_found)
				{
					missing_font_count ++;
					
					fl.outputPanel.trace(buffer);
				}
			}
		}
	}
	
	
	// --------------------------------
	// Summary info
	
	
	if(0 == missing_font_count)
	{
		fl.outputPanel.trace("None");
	}
}



/*
Function: fill
Returns: A string with a fixed with of len that starts with 'str' and ends with 'filler'.
Params:
	str - The base string.
	len - Desired length.
	filler - Desired filler character, ideally this is a single character.

fill("Hello, world", 20, ".") => "Hello, world......."
fill("Hello, world", 25, " ") => "Hello, world            "
fill("Hello, world", 6, "-") => "Hello-"
fill("Hello, world", 10, " ") => "Hello, wo "

*/
function fill(str, len, filler)
{
	var i = len;
	
	var filler_str = "";
	
	
	while(i--)
	{
		filler_str += filler;
	}
	
	
	str = str.substr(0, len - 1);
	
	filler_str = filler_str.substr(0, Math.max(0, len - str.length));
	
	
	return str + filler_str;
}



// ----------------------------------------------------------------------------
// Execute



var dom = fl.getDocumentDOM();

if(dom)
{
	findMissingFonts();
}
else
{
	alert("dom has no properties");
}
